package com.example.walkera.rtsp_android_client.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import com.example.walkera.rtsp_android_client.R;
import com.example.walkera.rtsp_android_client.function.ActivityCollector;
import com.example.walkera.rtsp_android_client.function.CameraInfoSharedPreferencesHelper;
import com.example.walkera.rtsp_android_client.function.WifiCheck;
import com.example.walkera.rtsp_android_client.global.GlobalInfo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by kaifa@lihaiping1603@aliyun.com on 2017/2/6.
 */
public class AirplaneChoose extends Activity {

    private final static String TAG="AirplaneChoose";
    private PlaneTypeHorizontalScrollView mPlaneTypeView;
    private PlaneTypeHorizontalScrollViewAdapter mPlaneTypeAdapter;
    private List<Integer> mPlaneTypeDatas = new ArrayList<Integer>(Arrays.asList(
                                        R.mipmap.vitus,
                                        R.mipmap.aibao,
                                        R.mipmap.voyager4));
    private List<String> mPlaneTypeNameList = new ArrayList<>(Arrays.asList("VITUS","AIBAO","VOYAGER4"));

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.airplane_choose);

        GlobalInfo.getInstance().setCurrentApp(this);

        mPlaneTypeView=(PlaneTypeHorizontalScrollView)findViewById(R.id.id_horizontalScrollView);
        mPlaneTypeAdapter=new PlaneTypeHorizontalScrollViewAdapter(this,mPlaneTypeDatas,mPlaneTypeNameList);
        //添加滚动回调
        mPlaneTypeView.setCurrentImageChangeListener(new PlaneTypeHorizontalScrollView.CurrentImageChangeListener()
        {
            @Override
            public void onCurrentImgChanged(int position,
                                            View viewIndicator)
            {
                viewIndicator.setBackgroundColor(Color.parseColor("#AA024DA4"));
            }
        });
        //添加点击回调
        mPlaneTypeView.setOnItemClickListener(new PlaneTypeHorizontalScrollView.OnItemClickListener()
        {
            @Override
            public void onClick(View view, int position)
            {
                view.setBackgroundColor(Color.parseColor("#AA024DA4"));
                //写选择的飞机型号
                CameraInfoSharedPreferencesHelper.getInstance().writeAirPlaneName(mPlaneTypeNameList.get(position));

//                if (WifiCheck.isWifiContected(GlobalInfo.getInstance().getAppContext())){
//                    //启动主页面
//                    Intent intent = new Intent(AirplaneChoose.this, MainActivity.class);
//                    startActivity(intent);
//                }
//                else {
                    //启动wifi连接页面
                    Intent intent = new Intent(AirplaneChoose.this,LHPWifiConnectActivity.class);
                    startActivity(intent);
//                }
            }
        });
        //设置适配器
        mPlaneTypeView.initDatas(mPlaneTypeAdapter);

        ActivityCollector.addActivity(this);
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_2://return，返回
                if (!MainActivity.getHomeVervison())
                    ActivityCollector.finishAll();
                break;
            case KeyEvent.KEYCODE_1://搜索
                break;
            case KeyEvent.KEYCODE_6://确认
                //写选择的飞机型号
                CameraInfoSharedPreferencesHelper.getInstance().writeAirPlaneName(mPlaneTypeNameList.get(mPlaneTypeView.getmCurrentSelectItemIndex()));

//                if (WifiCheck.isWifiContected(GlobalInfo.getInstance().getAppContext())){
//                    //启动主页面
//                    Intent intent = new Intent(AirplaneChoose.this, MainActivity.class);
//                    startActivity(intent);
//                }
//                else {
//                    //启动wifi连接页面
//                    Intent intent = new Intent(AirplaneChoose.this,LHPWifiConnectActivity.class);
//                    startActivity(intent);
//                }

                //启动wifi选择界面
                Intent intent=new Intent();
                intent.setClass(AirplaneChoose.this,LHPWifiConnectActivity.class);
                startActivity(intent);
                break;
            case KeyEvent.KEYCODE_7://down
                mPlaneTypeView.loadNextImg();
                break;
            case KeyEvent.KEYCODE_3://up
                mPlaneTypeView.loadPreImg();
                break;
            default:
                return super.onKeyDown(keyCode, event);
        }

        return true;
    }

}
